package com.ziyun.erp.web.modules.goods.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.ziyun.erp.common.utils.UserUtils;
import com.ziyun.erp.modules.goods.VO.TempOptionVO;
import com.ziyun.erp.modules.goods.entity.GoodsAttributeEntity;
import com.ziyun.erp.modules.goods.entity.GoodsOptionEntity;
import com.ziyun.erp.modules.goods.service.GoodsAttributeService;
import com.ziyun.erp.modules.goodsCategory.entity.GoodsCategoryEntity;
import com.ziyun.erp.modules.goodsCategory.service.GoodsCategoryService;
import io.renren.common.validator.ValidatorUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ziyun.erp.modules.goods.entity.GoodsTemplateEntity;
import com.ziyun.erp.modules.goods.service.GoodsTemplateService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 商品模板表
 *
 * @author
 * @email
 * @date 2018-08-20 09:57:04
 */
@RestController
@RequestMapping("goods/goodstemplate")
public class GoodsTemplateController {
    @Autowired
    private GoodsTemplateService goodsTemplateService;

    @Autowired
    private GoodsCategoryService goodsCategoryService;

    @Autowired
    private GoodsAttributeService goodsAttributeService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("goods:goodstemplate:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = goodsTemplateService.queryPage(params);
        List<GoodsTemplateEntity> list = (List<GoodsTemplateEntity>) page.getList();
//        List<GoodsTemplateEntity> allTemplate = goodsTemplateService.selectAllTemplate();
        goodsTemplateService.setAttributeNames(list);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{templateId}")
    @RequiresPermissions("goods:goodstemplate:info")
    public R info(@PathVariable("templateId") Long templateId){
        GoodsTemplateEntity goodsTemplate = goodsTemplateService.selectById(templateId);
//        //查分类
        GoodsCategoryEntity goodsCategory = goodsCategoryService.getCatByTemplateId(templateId);
        //根据attribute_ids和influence_craft_ids,查出所有的attribute对象作为list返回
        List<GoodsAttributeEntity> selectedAttributeList = goodsAttributeService.selectByIds(goodsTemplate.getAttributeIds());
        List<GoodsAttributeEntity> influenceCraftList = goodsAttributeService.selectByIds(goodsTemplate.getInfluenceCraftIds());
        Map<String, List> resultMap = null;
        if(goodsTemplate.getOptionJson() != null){
//            tempOptionsList = goodsTemplateService.handlerOptionJson(goodsTemplate.getOptionJson());
            resultMap = goodsTemplateService.handlerOptionJson(goodsTemplate);
        }
        return R.ok().put("goodsTemplate", goodsTemplate).put("selectedAttributeList",selectedAttributeList)
                .put("influenceCraftList",influenceCraftList).put("goodsCategory",goodsCategory)/*.put("tempOptionsList",tempOptionsList)*/
                .put("resultMap",resultMap);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("goods:goodstemplate:save")
    public R save(@RequestBody GoodsTemplateEntity goodsTemplate){
//        Integer id = goodsTemplateService.saveTemplate(goodsTemplate);
//        goodsTemplate.setCreateName(UserUtils.getCurentUser().getUsername());
//        goodsTemplateService.insert(goodsTemplate);
//        Integer categoryId = goodsTemplate.getCategoryId();
//        Long templateId = goodsTemplate.getTemplateId();
//        //更新分类表
////        if(categoryId != null){
//        goodsCategoryService.updateTemplateId(templateId,categoryId);
////        }else{
////            System.out.println("更新模板失败!");
////        }
        goodsTemplate.setCreateName(UserUtils.getCurentUser().getUsername());
        goodsTemplateService.save(goodsTemplate);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("goods:goodstemplate:update")
    public R update(@RequestBody GoodsTemplateEntity goodsTemplate){
        ValidatorUtils.validateEntity(goodsTemplate);
        goodsTemplateService.updateGoodsTemplate(goodsTemplate,UserUtils.getCurentUser().getUserId());
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("goods:goodstemplate:delete")
    public R delete(@RequestBody Long[] templateIds){
//        goodsTemplateService.deleteBatchIds(Arrays.asList(templateIds));
        goodsTemplateService.fagDeleteByTemplateIds(templateIds);
        return R.ok();
    }

    /**
     *
     */
    @RequestMapping("/loadTempOptionsList/{attributeIds}")
    public R loadTempOptionsList(@PathVariable("attributeIds")Long[] attributeIds){
        Map<String, List> resultMap = goodsTemplateService.loadTempOptionsList(attributeIds);
        return R.ok().put("resultMap",resultMap);
    }

}
